<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Crypt;
use DB;
use Hash;

class ZhuceController extends Controller
{
    public function index()
   	{
   		// return '注册';
   		return view('home.zhuce.zhuce');
   	}

   	public function doreg(Request $request)
    {
    	//dd($request -> all());
    	$data = $request -> all();
		$request -> flash();
		if(empty($data['email']))
		{
			return back() -> with(['em' => '邮箱不能为空']);
		}else if(empty($data['password']))
		{
			return back() -> with(['passwd' => '密码不能为空']);
		}else if(empty($data['repassword']))
		{
			return back() -> with(['repasswd' => '确认密码不能为空']);
		}else if( $data['password'] != $data['repassword'])
		{
			return back() -> with(['repasswd' => '两次密码不一致']);
		}else if(empty($data['code']))
		{
			return back() -> with(['ce' => '验证码不能为空']);
		}

		if($data['code'] != session('milkcaptcha')) {
            //用户输入验证码错误
            return back() -> with(['ce' => '您输入验证码错误']);
        }

		$data = $request -> except(['code','_token','repassword']);
		
	
		// dd($data);

		$rese = DB::table('users') -> where('email', $request -> input('email')) -> first(); 
		if ($rese) {
			return back() -> with(['em' => '邮箱已存在']);
		}
		
		$data['token'] = str_random(100);
		$data['name'] = 'mi-'.$data['email'];
		$data['password'] = Crypt::encrypt($data['password']);


		$res = DB::table('users') -> insert($data);
		return redirect('home/login');

		}



		

		
 }

